/******************************************************

  Project 1: Image Editor in OGRE

  National Taiwan University of Science and Technology
  Computer Game, Animation, and Graphics Laboratory

  Professor: Yu-Chi Lai

  Student:  Dobromir Todorov, Chun-Wei Wang, Damody		

******************************************************/

#pragma once

#include "stdafx.h"
#include "BaseApplication.h"
#include "Interface.h"
#include "ClassDefine.h"
#include "KinectController.h"
#include <vector>
#include <fstream>
#include <iterator>
#include "GameManager.h"
#include "PPSoundManager.h"

class Stroke { // Data structure for holding painterly strokes.
public:
	Stroke(void);
	Stroke(unsigned int radius, unsigned int x, unsigned int y,
		unsigned char r, unsigned char g, unsigned char b, unsigned char a);

	// data
	unsigned int radius, x, y;	// Location for the stroke
	unsigned char r, g, b, a;	// Color
};

class Main : public BaseApplication
{
public:
    Main(void);
    virtual ~Main(void);

	KinectController*						kinectController;
	Ogre::TexturePtr						texRenderTarget;
	DynamicImage*							kinectPlane;
	DynamicImage*							leftHandPic;
	DynamicImage*							rightHandPic;
	DynamicImage*							trashCan;
	DynamicImage*							hatPic;
	
	// Testing Hand Shaking
	/*
	DynamicImage*							DotLHand;
	DynamicImage*							DotRHand;
	DynamicImage*							DotLWrist;
	DynamicImage*							DotRWrist;
	*/

	Ogre::Viewport *mCamView;
	Ogre::Viewport* mViewPort;
	Ogre::SceneNode *mCamNode;   // The SceneNode the camera is currently attached to
	Ogre::Plane mPlane;	//Invisible plane on which we do the mouse dragging
	Ogre::SceneNode *mCurrentObject;	//pointer to our currently selected object
	GameManager* gm;
	Interface *mInterface;
	unsigned char* Main::To_RGB(void);
	void Main::RGBA_To_RGB(unsigned char *rgba, unsigned char *rgb);
	bool isMouseClick;
	OIS::MouseEvent* mouseEv;
	PPSoundManager* mPPSoundManager;

	enum StrokeType {FP01,FP02,FP03};
	StrokeType useStroke;
	int useBgImg;
	bool isLoadBackground;
	DynamicImage* drawingBackground;
protected:

    void createScene();
	virtual bool frameRenderingQueued(const Ogre::FrameEvent& evt);
	virtual void createFrameListener(void);
	bool mouseMoved(const OIS::MouseEvent &arg);
	bool mousePressed(const OIS::MouseEvent &arg, OIS::MouseButtonID id);
	bool mouseReleased(const OIS::MouseEvent &arg, OIS::MouseButtonID id);
	
	bool keyPressed( const OIS::KeyEvent &arg );
	bool keyReleased( const OIS::KeyEvent &arg );
	void addCharacter(wchar_t charc, float x, float y);
	void readStringFromFile();
	void setUpKinect();
	bool kinectMouseControl();
	void simulateMouseClick(int x, int y);
	void simulateMouseMove(int x, int y);
	void clickDetect(int handID);
	bool printImageFromFile( Ogre::String filePath );
	bool DetectEndPose();
	void endDrawing();
	void endDrawingNextFrame();
	bool endNextFrame;
	Ogre::Vector3 mDirection;     // Value to move in the correct direction
	
	Ogre::Real mMove;            // The movement constant
	Ogre::Real mRotate;            // The movement constant
	bool bAlt, bMidMouseDown, bLMouseDown, bRMouseDown, uKey,zKey,rKey;	//true if mouse buttons are held down

	bool yKeyPressed;	//Flag for moving objects in Y direction
	bool xKeyPressed;	//Flag for moving objects in XZ direction
	float mouseX, mouseY;
	Ogre::RaySceneQuery* mRaySceneQuery;
	int trailRenderingId;
	std::vector<Ogre::Vector2> handPosition[2];
	bool clickBreak[2];
	enum {bb, gg, rr, aa}; 
	enum {L_HAND_DETECT, R_HAND_DETECT}; 
	enum {BLACK = 0, WHITE = 255};
};